requirejs.config({
    baseUrl: "js",
    paths: {
    	zlib:"../lib/zlib.js-0.1.7/bin/zlib.min",
        lib: "../lib/js",
        jquery: "../lib/js/jquery-readable",
        jsrender: "../lib/js/jsrender-readable",
        jqueryui:"../lib/jquery-ui-1.10.3/ui/jquery-ui",
        datatables:"../lib/datatables/js/jquery.dataTables.readable",
        datatables_bootstrap:"../lib/datatables-bootstrap-plugin/js/dataTables.bootstrap",
        typeahead : "../lib/typeahead/js/typeahead",
        hogan : "../lib/hogan/js/hogan",
        validate : "../lib/validate/js/jquery.validate",
        validate_message_cn : "../lib/validate/js/messages_cn",
        validate_metadata : "../lib/validate/js/jquery.metadata",
        validate_add_method : "../lib/validate/js/additional-methods"
    },

    shim: {
      'zlib':{ 
　　　　　　　　exports: 'Zlib'
　　　　　　}

    }
});

// Start the main app logic. 
(function(window){

requirejs(
  [ "require",
    "jquery",
    "jsrender",
    "zlib",
    "datatables",
    "datatables_bootstrap",
    "bip.ver",
    "bip.config",
    "bip.util",
    "bip.base",
    "bip.cache",
    "bip.async"
  ],
  function(require, $) {
    "use strict";
    var $bip = require("bip.base");
    if (!$bip) {
      alert("内部异常(程序bug): $bip is undefined!");
    }
    var $ver = require("bip.ver");
    $bip.log("bip version: ", $ver.buildVersion);

    // the only place that register a global variable $bip/bipApp
    if(typeof window === "object" && typeof window.document === "object"){
      window.$bip = window.bipApp = $bip;
    }

    if (window.bipDebugCustomize) {
      window.bipDebugCustomize($, require("bip.config"));
      delete window.bipDebugCustomize;
    }

    $bip.rootPath = window.g_bipWebRoot;
    $bip.loginUser = window.g_loginUser;
    delete window.g_bipWebRoot;
    delete window.g_loginUser;
    $bip.log("bip web root path: ", $bip.rootPath);
    $bip.log("current user: ", $bip.loginUser.loginName, ", ", $bip.loginUser.displayName, ", ", $bip.loginUser.email, ", ", $bip.loginUser.department);
    var $cache = require("bip.cache");
    var diskCache = $cache.disk;

    (function checkAppVer($bip, $ver, diskCache){
       var cachedVersion = $cache.disk.get("bipweb_version") || "";
       $bip.log("get cached app ver from webstorage: ", cachedVersion);
       if ($ver.buildVersion != cachedVersion) {
          $bip.log("bip web version changed: ", cachedVersion, " -> ", $ver.buildVersion);
          diskCache.clear();
          diskCache.set("bipweb_version", $ver.buildVersion);
          diskCache.set("user", $bip.loginUser.loginName);
       }
       else {
         var cachedLoginName = diskCache.get("user");
         if (cachedLoginName && cachedLoginName != $bip.loginUser.loginName) {
           $bip.log("bip user changed: ", cachedLoginName, " -> ", $bip.loginUser.loginName);
           diskCache.clear(cachedLoginName);
           diskCache.set("user", $bip.loginUser.loginName);
         }
       }
    })($bip, $ver, diskCache);


    var $async = require("bip.async");
    
    function onDocumentReady($bip) {
      
		  
    }

    $(document).ready(function(){
      $bip.log("async cache enabled: " + require("bip.config").async_cache_enable);
      $bip.domReady = true;
      onDocumentReady($bip);
    });

  }
);

})(window);
